package com.jlpay.openapi.demo;


import com.jlpay.open.jlpay.sdk.java.config.NotifyConfig;
import com.jlpay.open.jlpay.sdk.java.config.OrgConfig;
import com.jlpay.open.jlpay.sdk.java.enums.CryptoAlgorithm;
import com.jlpay.open.jlpay.sdk.java.http.DefaultHttpClientBuilder;
import com.jlpay.open.jlpay.sdk.java.service.JlpayNotifyParseService;
import com.jlpay.open.jlpay.sdk.java.service.JlpayOrgService;
import com.jlpay.open.jlpay.sdk.java.service.JlpayUploadFileService;

/**
 * @author zhaomeixia
 * @since 2024/2/20
 */
public class JlpayOpenApiService {
    /**
     * UAT机构私钥
     */
    public static final String UAT_ORG_PRIVATE_KEY = "-----BEGIN EC PRIVATE KEY-----\n" +
            "MIGTAgEAMBMGByqGSM49AgEGCCqBHM9VAYItBHkwdwIBAQQgHqOp471SMDsQml2G\n" +
            "2ErpZPLD2X7rbMqa6P1O9Zqlhe+gCgYIKoEcz1UBgi2hRANCAAQkmEWaN4xGn0bB\n" +
            "KHG1a6e/pafL/+YODalye7SWUNC8hijk7dH5qXf016RyJnQEitPbVj9AmteAkijI\n" +
            "GvpJOaXq\n" +
            "-----END EC PRIVATE KEY-----";

    /**
     * UAT嘉联公钥
     */
    public static final String UAT_JLPAY_PUBLIC_KEY = "-----BEGIN PUBLIC KEY-----\n" +
            "MIIBMzCB7AYHKoZIzj0CATCB4AIBATAsBgcqhkjOPQEBAiEA/////v//////////\n" +
            "//////////8AAAAA//////////8wRAQg/////v////////////////////8AAAAA\n" +
            "//////////wEICjp+p6dn140TVqeS89lCafzl4n1FauPkt28vUFNlA6TBEEEMsSu\n" +
            "LB8ZgRlfmQRGajnJlI/jC7/yZgvhcVpFiTNMdMe8Nzai9PZ3nFm9zuNraSFT0KmH\n" +
            "fMYqR0AC3zLlITnwoAIhAP////7///////////////9yA99rIcYFK1O79Ak51UEj\n" +
            "AgEBA0IABKZ3VWwH0Aw9xvOWXb+xM2gXyApWEAZLFy8UyIigp3ehOTwPJ693njrg\n" +
            "NavTLdzAtFBqXRreivlwBTIFy8iRgn8=\n" +
            "-----END PUBLIC KEY-----";

    private static final JlpayOrgService jlpayClearingService = createJlpayOrgService();

    private static final JlpayUploadFileService jlpayUploadFileService = createJlpayUploadFileService();

    private static final JlpayNotifyParseService jlpayNotifyParseService = createJlpayNotifyParseService();

    private static final JlpayNotifyParseService jlpayOpenMerchNotifyParseService = createJlpayOpenMerchNotifyParseService();

    private static final JlpayOrgService jlpayOpenMerchService = createJlpayOpenMerchService();

    private static final JlpayOrgService jlpayOpenMerchWithCryptoService = createJlpayOpenMerchWithCryptoService();


    private JlpayOpenApiService() {
        throw new IllegalStateException("Utility class");
    }

    public static JlpayOrgService clearingService() {
        return jlpayClearingService;
    }

    public static JlpayUploadFileService uploadFileService() {
        return jlpayUploadFileService;
    }

    public static JlpayNotifyParseService notifyParseService() {
        return jlpayNotifyParseService;
    }

    public static JlpayNotifyParseService openMerchNotifyParseService() {
        return jlpayOpenMerchNotifyParseService;
    }

    public static JlpayOrgService openMerchService() {
        return jlpayOpenMerchService;
    }

    public static JlpayOrgService openMerchWithCryptoService() {
        return jlpayOpenMerchWithCryptoService;
    }

    private static JlpayOrgService createJlpayOrgService() {
        OrgConfig orgConfig = OrgConfig.builder()
                .appId("1vzougr30f9vcw")
                .url("https://openapi-uat.jlpay.com")
                .orgPriKey(UAT_ORG_PRIVATE_KEY)
                .jlpayPubKey(UAT_JLPAY_PUBLIC_KEY)
                .build();
        return new JlpayOrgService.Builder()
                .config(orgConfig)
                // 可以自定义HttpClient, 也可以不传, 默认使用DefaultHttpClientBuilder
                .httpClient(new DefaultHttpClientBuilder()
                        .config(orgConfig)
                        .connectTimeoutMills(3000)
                        .readTimeoutMills(5000)
                        .writeTimeoutMills(5000)
                        .build())
                .build();
    }


    private static JlpayUploadFileService createJlpayUploadFileService() {
        OrgConfig orgConfig = OrgConfig.builder()
                .appId("1vzougr30f9vcw")
                .url("http://upload-uat.jlpay.com")
                .orgPriKey(UAT_ORG_PRIVATE_KEY)
                .jlpayPubKey(UAT_JLPAY_PUBLIC_KEY)
                .build();

        return new JlpayUploadFileService.Builder()
                .httpClient(new DefaultHttpClientBuilder()
                        .config(orgConfig)
                        .connectTimeoutMills(5000)
                        .readTimeoutMills(5000)
                        .writeTimeoutMills(5000)
                        .build())
                .config(orgConfig)
                .build();


    }


    private static JlpayNotifyParseService createJlpayNotifyParseService() {
        NotifyConfig config = NotifyConfig.builder()
                .jlpayPubKey(UAT_JLPAY_PUBLIC_KEY)
                .build();
        return new JlpayNotifyParseService.Builder().config(config).build();
    }

    private static JlpayNotifyParseService createJlpayOpenMerchNotifyParseService() {
        NotifyConfig config = NotifyConfig.builder()
                .jlpayPubKey(UAT_JLPAY_PUBLIC_KEY)
                .build();
        return new JlpayNotifyParseService.Builder().config(config).build();
    }

    private static JlpayOrgService createJlpayOpenMerchService() {
        OrgConfig orgConfig = OrgConfig.builder()
                .appId("1vzougr30f9vcw")
                .url("https://openapi-uat.jlpay.com")
                .orgPriKey(UAT_ORG_PRIVATE_KEY)
                .jlpayPubKey(UAT_JLPAY_PUBLIC_KEY)
                .verifyMerchNo(Boolean.FALSE)
                .verifyOrgNo(Boolean.FALSE)
                .build();
        return new JlpayOrgService.Builder()
                .config(orgConfig)
                // 可以自定义HttpClient, 也可以不传, 默认使用DefaultHttpClientBuilder
                .httpClient(new DefaultHttpClientBuilder()
                        .config(orgConfig)
                        .connectTimeoutMills(3000)
                        .readTimeoutMills(5000)
                        .writeTimeoutMills(5000)
                        .build())
                .build();
    }

    private static JlpayOrgService createJlpayOpenMerchWithCryptoService() {
        OrgConfig orgConfig = OrgConfig.builder()
                .appId("1vzougr30f9vcw")
                .url("https://openapi-uat.jlpay.com")
                .orgPriKey(UAT_ORG_PRIVATE_KEY)
                .jlpayPubKey(UAT_JLPAY_PUBLIC_KEY)
                .verifyMerchNo(Boolean.FALSE)
                .verifyOrgNo(Boolean.FALSE)
                .cryptoAlg(CryptoAlgorithm.SM2_WITH_SM4.getCode())
                .autoEncrypt(Boolean.TRUE)
                .autoDecrypt(Boolean.TRUE)
                .build();
        return new JlpayOrgService.Builder()
                .config(orgConfig)
                // 可以自定义HttpClient, 也可以不传, 默认使用DefaultHttpClientBuilder
                .httpClient(new DefaultHttpClientBuilder()
                        .config(orgConfig)
                        .connectTimeoutMills(3000)
                        .readTimeoutMills(5000)
                        .writeTimeoutMills(5000)
                        .build())
                .build();
    }
}
